package com.nondo.rdp.finance.logic.SettingModule;

import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.StringUtils;
import com.nondo.rdp.finance.domain.analysis.Analysis;
import com.nondo.rdp.finance.domain.course.Course;
import com.nondo.rdp.finance.domain.course.Section;
import com.nondo.rdp.finance.service.analysis.AnalysisService;
import com.nondo.rdp.finance.service.course.CourseService;
import com.nondo.rdp.finance.service.course.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 专门用于处理设置模块逻辑类
 */
@Component
public class SettingModuleLogic<T> {
    private final static int TYPE_DEFAULT = 0;//不进行任何操作
    private final static int TYPE_URL = 1;//打开连接
    private final static int TYPE_ANALYSIS_LIST = 10;//打开分析列表
    private final static int TYPE_ANALYSIS_INFO = 11;//打开分析详情
    private final static int TYPE_COURSE_LIST = 20;//打开课程列表
    private final static int TYPE_COURSE_INFO = 21;//打开课程详情
    private final static int TYPE_COURSE_CUSTOM_LIST = 22;//打开自定义课程列表
    private final static int TYPE_ACTIVITY_LIST = 30;//打开活动列表
    private final static int TYPE_STUDY = 31;//打开学股堂
    private final static int TYPE_STOCK = 32;//打开模拟炒股
    private final static int TYPE_OPEN_LIST = 40;//打开证券开户列表

    public final static int STYLE_ONE = 1;//样式一
    public final static int STYLE_TWO = 2;//样式二
    public final static int STYLE_THREE = 3;//样式三

    @Autowired
    AnalysisService analysisService;

    @Autowired
    CourseService courseService;

    @Autowired
    SectionService sectionService;

    /**
     * 返回更多的逻辑处理列表
     * 0=不展示不操作，1=打开url地址
     * 10=打开分析列表，11=打开分析详情
     * 20=打开课程列表，21=打开课程详情
     *
     * @return
     */
    public int[] getHandTypeList() {
        int[] handTypeList = {
                TYPE_DEFAULT,
                TYPE_URL,
                TYPE_ANALYSIS_LIST,
                TYPE_ANALYSIS_INFO,
                TYPE_COURSE_LIST,
                TYPE_COURSE_INFO,
                TYPE_COURSE_CUSTOM_LIST,
                TYPE_ACTIVITY_LIST,
                TYPE_STUDY,
                TYPE_STOCK,
                TYPE_OPEN_LIST
        };
        return handTypeList;
    }

    /**
     * 检查值是否存在于更多可处理列表中
     *
     * @param value
     * @return
     */
    public boolean checkType(int value) {
        int[] handTypeList = this.getHandTypeList();
        int result = Arrays.binarySearch(handTypeList, value);
        if (result >= 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查添加更新的类型和值，防止错误操作导致app崩溃
     *
     * @param type  int 类型
     * @param value string 值
     * @return
     */
    public boolean checkTypeValue(int type, String value) {
        boolean checkType = this.checkType(type);
        if (!checkType) {//不存在的类型
            return false;
        } else {
            if (type == TYPE_DEFAULT || type == TYPE_ACTIVITY_LIST || type == TYPE_STUDY || type == TYPE_STOCK || type == TYPE_OPEN_LIST) {//这几种情况可以为空
            } else {
                if (value.equals("")) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 处理类型，返回对应类型所需要的内容
     *
     * @param type
     * @param value
     * @return
     */
    public Map getTypeContent(int type, String value) {
        Map extraMap = new HashMap();
        if (StringUtils.isEmpty(value)) {
            return extraMap;
        }
        //处理更多类型的数据
        switch (type) {
            case 11://打开分析详情
                Analysis analysis = this.analysisService.findOne(Long.parseLong(value));
                if (analysis != null) {
                    extraMap.put("id", analysis.getId());
                    extraMap.put("title", analysis.getTitle());
                    extraMap.put("image", analysis.getMainImg());
                    extraMap.put("readNum", analysis.getReadNum());
                    extraMap.put("publishDate", CommUtil.dateFormat(analysis.getCreateDate(), "MM.dd"));
                    extraMap.put("commentNum", analysis.getCommentNum());
                }
                break;
            case 20://打开课节详情
                Section section = this.sectionService.findOne(Long.parseLong(value));
                if (section != null) {
                    extraMap.put("id", section.getId());
                    extraMap.put("title", section.getName());
                    extraMap.put("image", CommUtil.getFileUrl(section.getMainImg()));
                    extraMap.put("publishDate", CommUtil.dateFormat(section.getPublishDate(), "MM.dd"));
                    extraMap.put("readNum", section.getReadNum());
                    extraMap.put("videoId", section.getAttach().getVideoId());
                }
                break;
            case 21://打开课程详情
                Course course = this.courseService.findOne(Long.parseLong(value));
                if (course != null) {
                    extraMap.put("id", course.getId());
                    extraMap.put("title", course.getName());
                    extraMap.put("image", CommUtil.getFileUrl(course.getMainImg()));
                    extraMap.put("publishDate", CommUtil.dateFormat(course.getPublishDate(), "MM.dd"));
                    extraMap.put("rate", course.getRate());
                }
                break;
            default:
                //值为0,1,2,10,22均只需要展示APP自动处理和取value里面的值
        }

        //解决找不到内容或者没有操作行为的参数缺失bug
        if (!extraMap.containsKey("id")) {
            extraMap.put("id", 0);
        }
        if (!extraMap.containsKey("title")) {
            extraMap.put("title", "");
        }
        if (!extraMap.containsKey("image")) {
            extraMap.put("image", new HashMap());
        }
        if (!extraMap.containsKey("readNum")) {
            extraMap.put("readNum", 0);
        }
        if (!extraMap.containsKey("videoId")) {
            extraMap.put("videoId", 0);
        }
        if (!extraMap.containsKey("rate")) {
            extraMap.put("rate", 0.0);
        }
        if (!extraMap.containsKey("publishDate")) {
            extraMap.put("publishDate", "");
        }
        if (!extraMap.containsKey("commentNum")) {
            extraMap.put("commentNum", 0);
        }

        return extraMap;
    }

    /**
     * 用于后端的类型数据处理
     *
     * @param type
     * @param value
     * @return
     */
    public Map getAdminTypeContent(int type, String value) {
        Map extraMap = new HashMap();
        if (StringUtils.isEmpty(value)) {
            return extraMap;
        }
        Course course = new Course();
        //处理更多类型的数据
        switch (type) {
            case 11://打开分析详情
                Analysis analysis = this.analysisService.findOne(Long.parseLong(value));
                if (analysis != null) {
                    extraMap.put("id", analysis.getId());
                    extraMap.put("title", analysis.getTitle());
                    extraMap.put("image", analysis.getMainImg());
                }
                break;
            case 20://打开课节详情
                Section section = this.sectionService.findOne(Long.parseLong(value));
                if (section != null) {
                    extraMap.put("id", section.getId());
                    extraMap.put("title", section.getName());
                    extraMap.put("image", CommUtil.getFileUrl(section.getMainImg()));
                }
                break;
            case 21://打开课程详情
                course = this.courseService.findOne(Long.parseLong(value));
                if (course != null) {
                    extraMap.put("id", course.getId());
                    extraMap.put("title", course.getName());
                    extraMap.put("image", CommUtil.getFileUrl(course.getMainImg()));
                }
                break;
            case 22://打开自定义课程列表
                List courseList = new ArrayList();
                String[] courseIds = value.split(",");
                for (String item : courseIds) {
                    Map itemMap = new HashMap();
                    course = this.courseService.findOne(Long.parseLong(item));
                    if (course != null && course.getStatus() == 1) {
                        itemMap.put("id", course.getId());
                        itemMap.put("title", course.getName());
                        itemMap.put("image", CommUtil.getFileUrl(course.getMainImg()));
                        courseList.add(itemMap);
                    }
                }
                extraMap.put("list", courseList);
                break;
            default:
                //值为0,1,2,10均只需要展示APP自动处理和取value里面的值
        }

        return extraMap;
    }

    /**
     * 检查传递的样式参数是否正确
     *
     * @param style
     * @return
     */
    public boolean checkStyle(int style) {
        if (style == this.STYLE_ONE && style == this.STYLE_TWO) {
            return true;
        }

        return false;
    }

    /**
     * 获取样式列表
     *
     * @return
     */
    public List getStyleList() {
        List list = new ArrayList();
        list.add(this.STYLE_ONE);
        list.add(this.STYLE_TWO);
        return list;
    }
}
